
<template>
  <el-form
    ref="staffForm"
    :model="form"
    :size="size"
    :rules="rules"
    :label-suffix="labelSuffix"
    :label-position="labelPosition"
    :label-width="labelWidth"
    @submit.native.prevent
  >
    <el-form-item
      label="人员单位"
      prop="company_id"
    >
      <el-cascader
        v-model="form.company_id"
        placeholder="可按单位名称搜索"
        :options="companys"
        :props="{value:'id', label:'name', emitPath:false}"
        filterable
        clearable
      />
    </el-form-item>
    <el-form-item
      ref="formInput.realname"
      label="姓名"
      prop="realname"
      class="form-item--medium"
    >
      <el-input v-model="form.realname" />
    </el-form-item>
    <el-form-item
      ref="formInput.mobile"
      label="手机号"
      prop="mobile"
      class="form-item--medium"
    >
      <el-input
        v-model="form.mobile"
        maxlength="11"
        show-word-limit
      />
    </el-form-item>

    <slot name="action">
      <form-action v-if="actionVisible">
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
          >{{ submitButtonText }}</el-button>
        </el-form-item>
      </form-action>
    </slot>
  </el-form>
</template>

<script>
import { objectFillMerge } from '@/utils'
import FormAction from '@/components/Form/FormAction'
export default {
  name: 'ImportForm',
  components: {
    FormAction
  },
  props: {
    primaryKey: {
      type: [String, Number],
      default: 0
    },
    labelPosition: {
      type: String,
      default: ''
    },
    labelWidth: {
      type: String,
      default: '20%'
    },
    labelSuffix: {
      type: String,
      default: '：'
    },
    size: {
      type: String,
      default: 'small'
    },
    submitButtonText: {
      type: String,
      default: '提交保存'
    },
    actionVisible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      form: {
        realname: '',
        mobile: '',
        company_id: '',
        status: 0
      },
      companys: [],
      rules: {
        realname: [{ required: true, message: '请输入人员姓名', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入人员手机号', trigger: 'blur' }],
        company_id: [{ required: true, message: '请设置人员隶属单位', trigger: 'change' }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.onLoad()
  },
  mounted() {
    console.log('on mounted form')
  },
  methods: {
    onLoad() {
      this.$http.work.updateStaff(this.form, this.primaryKey, false).then(res => {
        const { form, companys } = res.data
        this.form = objectFillMerge(this.form, form)
        this.companys = companys
      })
    },
    onSubmit() {
      this.$refs.staffForm.validate((valid, obj) => {
        if (valid) {
          console.log(this.form)
          this.$http.work.updateStaff(this.form, this.primaryKey).then(res => {
            this.$emit('submit', res.data)
          })
        } else {
          let firstErrorElname = ''
          for (const i in obj) {
            firstErrorElname = `formInput.${i}`
            break
          }
          var dom = this.$refs[firstErrorElname]
          console.log(dom, this.$refs)
          if (dom) {
            dom.$refs.input && dom.$refs.input.focus()
            dom.$el.scrollIntoView({
              // 滚动到指定节点
              block: 'center', // 值有start,center,end，nearest，当前显示在视图区域中间
              behavior: 'smooth' // 值有auto、instant,smooth，缓动动画（当前是慢速的）
            })
          }
          console.log('error submit!!', this.form)
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
.brand-upload {
  &__thumb,
  &__cover {
    .upload-square {
      background: #f2f3f7;
    }
  }
}
</style>
